package com.atlansheng.rocketmqdemoboot.demos.conf;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.ServiceState;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @ClassName: RocketMqHealthCheck
 * @Description:
 * @Date 2024/04/07 16:09
 * @Author songshaoqiu
 **/
@Component
@RequiredArgsConstructor
@Slf4j
public class RocketMqHealthCheck extends AbstractHealthIndicator {
    private final MyRocketMqTemplete rocketMQTemplate;


    protected void doHealthCheck(Health.Builder builder) throws Exception {
        try {
            Message<String> test = MessageBuilder.withPayload("test").build();
            SendResult sendResult = rocketMQTemplate.syncSend("test-topic", test);
            log.info("心跳检测详情{}",sendResult);
            builder.up();
        }catch (Exception e){
            e.printStackTrace();
            builder.down();

        }
    }

}
